export default {
  data: {
    arr:[], //15*15落棋位置
    box_arr:[], //14*14棋盘
    holder: '1',  //持棋者 1：黑棋 -1：白棋，持黑先走
    arr1:[], // 记录当前符合赢棋落子数组
    isEnd: false, //是否结束
    locate: null,
    message: '你赢了',
    colOne: [],
    colEnd: [],
  },
  onInit() {
    this.arr = new Array(225).fill('');
    this.box_arr = new Array(196).fill('');
    for(let i = 1; i < 15; i++){
      this.colOne.push(15 * i)
    }
    for(let j = 0; j < 15; j++){
      this.colEnd.push(14 + (15 * j))
    }
  },
  onShow(){
  },
  // 重新开始
  reSet(){
    this.arr = new Array(225).fill('');
    this.isEnd = false;
    this.holder = '1';
    this.arr1 = [];
  },
  play(e){
    if(this.isEnd) return;
    if(this.arr[e] == '1' || this.arr[e] == '-1'){
      console.log('该处已经有棋了');
      return false;
    }
    // 针对数组内的数据修改，请使用splice方法生效数据绑定变更
    this.arr.splice(e,1,this.holder);

    // 横向赢方法
    this.compute(e, 1, this.arr1);
    this.compute(e, -1, this.arr1);
    this.getResult(this.arr1);

    // 竖向赢方法
    this.compute(e, 15, this.arr1);
    this.compute(e, -15, this.arr1);
    this.getResult(this.arr1);

    //右斜赢方法
    this.compute(e, 14, this.arr1);
    this.compute(e, -14, this.arr1);
    this.getResult(this.arr1);

    // 左斜赢方法
    this.compute(e, 16, this.arr1);
    this.compute(e, -16, this.arr1);
    this.getResult(this.arr1);

    //    交换走棋
    this.holder = this.holder == '1' ? '-1' : '1';
  },

  // 查找统计符合条件的棋子
  compute(id,d,arr){
    id = parseInt(id);
    if(this.arr[id + d] && this.arr[id + d] == this.holder && this.scree(id+d,d)){
      arr.push(id);
      this.compute(id + d, d, arr);
    } else{
      arr.push(id);
    }
  },
  // 筛选
  scree(id,d){
    if((d == 1 && this.colOne.indexOf(id) > -1)
      || (d == -1 && this.colEnd.indexOf(id) > -1)
      || (d == 14 && this.colEnd.indexOf(id) > -1)
      || (d == -14 && this.colOne.indexOf(id) > -1)
      || (d == 16 && this.colOne.indexOf(id) > -1)
      || (d == -16 && this.colEnd.indexOf(id) > -1)){
      return false;
    }
    return true;
  },
  // 判断是否赢了
  getResult(arr){
    if(arr.length > 5){
      this.message = this.isWin(this.holder) + '赢了';
      arr.length = 0;
    } else {
      arr.length = 0;
    }
  },
  isWin(holder){
    this.isEnd = true;
    if(holder == '1'){
      return '黑棋'
    } else {
      return '白棋'
    }
    console.log('游戏结束')
  },
}
